package com.suwish.pc.ui.preference.impl;

import static com.suwish.pc.util.ResourceUtils._Icon;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;

import com.suwish.pc.Setting;
import com.suwish.pc.ui.component.KeyValueItem;
import com.suwish.pc.ui.preference.Preference;
import com.suwish.pc.ui.util.UIHelper;
import com.suwish.pc.util.StringUtils;

/**
 * 
 * 系统配置
 * 
 * @author Min
 *
 */
public class SystemConfigPreference extends JPanel implements Preference {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JComboBox adbComboBox = null;
	private JComboBox lfComboxBox = null;
	private JComboBox mainWidowCloseComboxBox = null;
	public SystemConfigPreference(){
		initUI();
	}
	
	private void initUI(){
		setLayout(new GridBagLayout());
		add(new JLabel("ADB mode:"), new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.NORTHEAST,
				GridBagConstraints.NONE, new Insets(10, 10, 0, 0), 0, 0));
		adbComboBox = new JComboBox();
		adbComboBox.addItem(new KeyValueItem("Socket(Default)", "socket"));
		adbComboBox.addItem(new KeyValueItem("Adb", "adb"));
		add(adbComboBox, new GridBagConstraints(1, 0, 1, 1, 0, 0, GridBagConstraints.NORTHWEST,
				GridBagConstraints.HORIZONTAL, new Insets(10, 5, 0, 0), 0, 0));
		add(new JLabel("need restart"), new GridBagConstraints(3, 0, 1, 1, 1, 0, GridBagConstraints.NORTHEAST,
				GridBagConstraints.HORIZONTAL, new Insets(10, 5, 0, 0), 0, 0));
		
		add(new JLabel("Look&Feel:"), new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.NORTHEAST,
				GridBagConstraints.NONE, new Insets(10, 10, 0, 0), 0, 0));		
		lfComboxBox = new JComboBox();
		lfComboxBox.addItem(new KeyValueItem("Native(Windows Default)", UIManager.getSystemLookAndFeelClassName()));
		lfComboxBox.addItem(new KeyValueItem("Crocss Platform(Linux Default)", UIManager.getCrossPlatformLookAndFeelClassName()));
		add(lfComboxBox, new GridBagConstraints(1, 1, 1, 1, 0, 0, GridBagConstraints.NORTHWEST,
				GridBagConstraints.HORIZONTAL, new Insets(10, 5, 0, 0), 0, 0));
		add(new JLabel("need restart"), new GridBagConstraints(3, 1, 1, 1, 1, 0, GridBagConstraints.NORTHEAST,
				GridBagConstraints.HORIZONTAL, new Insets(10, 5, 0, 0), 0, 0));
		
		add(new JLabel("Window close:"), new GridBagConstraints(0, 2, 1, 1, 0, 0, GridBagConstraints.NORTHEAST,
				GridBagConstraints.NONE, new Insets(10, 10, 0, 0), 0, 0));		
		mainWidowCloseComboxBox = new JComboBox();
		mainWidowCloseComboxBox.addItem(new KeyValueItem("Exit(Default)", "1"));
		mainWidowCloseComboxBox.addItem(new KeyValueItem("Minimize", "2"));
		add(mainWidowCloseComboxBox, new GridBagConstraints(1, 2, 1, 1, 0, 0, GridBagConstraints.NORTHWEST,
				GridBagConstraints.HORIZONTAL, new Insets(10, 5, 0, 0), 0, 0));
		
		add(new JLabel(), new GridBagConstraints(100, 100, 100, 1, 1, 1, GridBagConstraints.WEST,
				GridBagConstraints.BOTH, new Insets(10, 10, 0, 0), 0, 0));
	}
	
	@Override
	public void load() {
		String adbMode = Setting.get("adb.mode");
		if(!StringUtils.isEmpty(adbMode)){
			int index = UIHelper.getKeyValueItemIndex(adbComboBox, adbMode);
			if(index != -1)adbComboBox.setSelectedIndex(index);
		}
		String lookFeel = Setting.get("lookfeel");
		if(!StringUtils.isEmpty(lookFeel)){
			int index = UIHelper.getKeyValueItemIndex(lfComboxBox, lookFeel);
			if(index != -1)lfComboxBox.setSelectedIndex(index);
		}
		String windClose = Setting.get("window.close");
		if(!StringUtils.isEmpty(windClose)){
			int index = UIHelper.getKeyValueItemIndex(mainWidowCloseComboxBox, windClose);
			if(index != -1)mainWidowCloseComboxBox.setSelectedIndex(index);
		}
	}

	@Override
	public void save() {
		Setting.set("adb.mode", ((KeyValueItem)adbComboBox.getSelectedItem()).getValue().toString());
		Setting.set("lookfeel", ((KeyValueItem)lfComboxBox.getSelectedItem()).getValue().toString());
		Setting.set("window.close", ((KeyValueItem)mainWidowCloseComboxBox.getSelectedItem()).getValue().toString());
	}
	@Override
	public JComponent getComponent() {
		return this;
	}

	@Override
	public ImageIcon getIcon() {
		return _Icon("Gnome-System-Run_64x64.png");
	}

	@Override
	public String getTitle() {
		return "System Config";
	}

	@Override
	public String getToolTip() {
		return "System Config";
	}

}
